<?php

define("IN_APP", true);
include("include/init.php");

$model_path = "model";

$request_page = trim(@$_GET["request_page"]);
$location_hash = trim(@$_GET["location_hash"]);

// 引数が正しく渡されてるか確認する
function check_arguments() {
	$num = func_num_args();
	
	for ($i = 0; $i < $num; $i++) {
		if (empty($_GET[func_get_arg($i)]))
			exit("insufficient parameters");
	}
	
	return true;
}

// POSTデータがあれば、処理する
if (!empty($_POST["post_data"])) {
	switch ($_POST["action"]) {
		case 'show_help':
			include("include/show_help.php");
			break;
		case 'create_group':
			include("include/group_create.php");
			break;
		case 'profile':
			include("include/profile_update.php");
			break;
		case 'twitter_list_create':
			include("include/create_twitter_list.php");
			break;
		case 'member_sort':
			include("include/member_sort.php");
			break;
		case 'group_delete':
			include("include/group_delete.php");
			break;
		case 'account_check':
			include("include/group_edit_functions.php");
			break;
		case 'account_add':
			include("include/group_edit_functions.php");
			break;
		case 'multi_account_add':
			include("include/group_edit_functions.php");
			break;
		case 'account_remove':
			include("include/group_edit_functions.php");
			break;
		case 'nickname_update':
			include("include/group_nickname_update.php");
			break;
		case 'tweet_post':
			include("include/tweet_post.php");
			break;
		case 'attendance_check':
			include("include/attendance_post.php");
			break;
		case 'history_search':
			include("include/history_search.php");
			ajaxHistoryResults();
			break;
		case 'attendance_update':
			include("include/attendance_update.php");
			break;
		case 'member_attendance_status_update':
			include("include/attendance_status_update.php");
			break;
	}
	exit();
}
// ハッシュタグで指定されているページをロードする
else if (!empty($location_hash)) {
	// 先頭のスラッシュを外す
	while ($location_hash[0] == "/")
		$location_hash = substr($location_hash, 1);
	
	$location_hash = explode("/", $location_hash);
	$location = $location_hash[0];
	
	if (isset($location_hash[1]))
		$subpage = $location_hash[1];
	else
		$subpage = "";
	
	// 正しいページに転送する
	switch ($location) {
		case 'グループ':
			$tab_number = 0;
			$page_name = "$model_path/group/group.tab.php";
			break;
		case 'ログアウト':
			$tab_number = 0;
			$page_name = "$model_path/login/logout.php";
			break;
		case 'プロフィール':
			$tab_number = 1;
			$page_name = "$model_path/profile/profile.tab.php";
			break;
		case '機能':
			$tab_number = 2;
			$page_name = "$model_path/feature/feature.tab.php";
			break;
		case '出欠':
			$tab_number = 2;
			$page_name = "$model_path/feature/attendance.php";
			break;
		case '履歴':
			$tab_number = 2;
			$page_name = "$model_path/feature/history.php";
			break;
		case '挙手':
			$tab_number = 2;
			$page_name = "$model_path/feature/check.php";
			break;
		case '挙手表示':
			$tab_number = 2;
			$page_name = "$model_path/feature/check_show.php";
			break;
		case 'アンケート':
			$tab_number = 2;
			$page_name = "$model_path/feature/vote.php";
			break;
		case 'アンケート表示':
			$tab_number = 2;
			$page_name = "$model_path/feature/vote_show.php";
			break;
		case 'グループ作成':
			$tab_number = 0;
			$page_name = "$model_path/group/group_create.php";
			break;
		case 'グループ編集':
			$tab_number = 0;
			$page_name = "$model_path/group/group_edit.php";
			break;
		// 見つからない場合はグループタブへ
		default:
			$tab_number = 0;
			$page_name = "$model_path/group/group.tab.php";
	}
	
	echo $tab_number . ";" . $page_name . ";" . $subpage;
	exit();
}
// 指定されたページをロードする
else if (!empty($request_page)) {
	switch ($request_page) {
		case 'view_group_menu':
			if (check_arguments("group_id")) {
				selectGroup($_GET["group_id"]);
				require("$model_path/group/view_group_menu.php");
			}
			break;
		case 'edit_group':
			require("$model_path/group/edit_group.php");
			break;
		case 'sort_members':
			require("$model_path/group/group_member_sort.php");
			break;
		case 'delete_group':
			include("$model_path/group/group_delete.php");
			break;
		case 'show_friends':
			$type = "friends";
			require("$model_path/group/group_edit_friends.php");
			break;
		case 'show_followers':
			$type = "followers";
			require("$model_path/group/group_edit_friends.php");
			break;
		case 'tweet_post':
			require("$model_path/feature/tweet_post.php");
			break;
		case 'logout':
			$logout = true;
			require("$model_path/login/login.tab.php");
			break;
		default:
			echo "指定されたページが見つかりません。";
	}
}
else {
	exit("エラー：ページが指定されませんでした。");
}

?>